package com.sptpc.mysharedpreferences;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;

import android.Manifest;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    Button save;
    Button read;
    EditText editText;
    TextView textView;
    private static final int PERMISSION_REQUEST = 1;
    private static String[] PERMISSION = {
            Manifest.permission.READ_EXTERNAL_STORAGE,
            Manifest.permission.WRITE_EXTERNAL_STORAGE,
            Manifest.permission.MOUNT_FORMAT_FILESYSTEMS,
            Manifest.permission.MOUNT_UNMOUNT_FILESYSTEMS,
    };
    private String MY_SHARE_PREFERENCE = "JinxZhang";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        save = findViewById(R.id.button);
        read = findViewById(R.id.button2);
        editText = findViewById(R.id.et);
        textView = findViewById(R.id.textView);
        checkPermission();
        save.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                toSaveSharedPreferences();
            }
        });
        read.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                fromReadSharedPreferences();
            }
        });
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == 100) {

            if (grantResults[0] == PackageManager.PERMISSION_DENIED ||
                    grantResults[1] == PackageManager.PERMISSION_DENIED)
                finish();
        }
    }

    private void checkPermission() {
        int permission = ActivityCompat.checkSelfPermission(this,
                Manifest.permission.WRITE_EXTERNAL_STORAGE);
        if (permission != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(this, PERMISSION, 100);
        }
    }
    private void toSaveSharedPreferences()
    {
        String content = editText.getText().toString();
        SharedPreferences sp = getSharedPreferences(MY_SHARE_PREFERENCE, MODE_PRIVATE);
        SharedPreferences.Editor editor = sp.edit();
        editor.putString("UserContent", content);
        editor.commit();    //内存写入文件
        Toast.makeText(MainActivity.this, "存储成功", Toast.LENGTH_SHORT).show();
    }
    private void  fromReadSharedPreferences(){

        SharedPreferences sp = getSharedPreferences(MY_SHARE_PREFERENCE, MODE_PRIVATE);
        SharedPreferences.Editor editor = sp.edit();
        String content = sp.getString("UserContent",null);
        textView.setText(content);
    }
}
